package com.ruoyi.web.controller.order;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.order.domain.MesProdRecord;
import com.ruoyi.web.controller.order.vo.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.MesProductNodeResult;
import com.ruoyi.order.service.IMesProductNodeResultService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 节点检测结果Controller
 *
 * @author guoxl
 * @date 2025-04-26
 */
@RestController
@RequestMapping("/order/result")
public class MesProductNodeResultController extends BaseController {
    @Autowired
    private IMesProductNodeResultService mesProductNodeResultService;

    /**
     * 查询节点检测结果列表
     */
    @PreAuthorize("@ss.hasPermi('order:result:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesProductNodeResult mesProductNodeResult) {
        startPage();
        List<MesProductNodeResult> list = mesProductNodeResultService.selectMesProductNodeResultList(mesProductNodeResult);
        return getDataTable(list);
    }

    /**
     * 导出节点检测结果列表
     */
    @PreAuthorize("@ss.hasPermi('order:result:export')")
    @Log(title = "节点检测结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesProductNodeResult mesProductNodeResult) {
        List<MesProductNodeResult> list = mesProductNodeResultService.selectMesProductNodeResultList(mesProductNodeResult);
        List<OP02_1DataFirstExport> op02_1DataFirstExportResult = new ArrayList<>();
        List<OP02_2DataExport> op02_2DataExportResult = new ArrayList<>();
        List<OP03DataExport> op03DataExportResult = new ArrayList<>();
        List<OP04DataExport> op04DataExportResult = new ArrayList<>();
        List<OP05DataExport> op05DataExportResult = new ArrayList<>();
        //总装线
        List<OP27DataFirstExport> op27DataFirstExportResult = new ArrayList<>();
        List<OP07DataExport> op07DataExportResult = new ArrayList<>();
        List<OP09DataExport> op09DataExportResult = new ArrayList<>();
        List<OP11DataExport> op11DataExportResult = new ArrayList<>();
        List<OP12DataExport> op12DataExportResult = new ArrayList<>();
        List<OP14DataExport> op14DataExportResult = new ArrayList<>();
        List<OP15DataExport> op15DataExportResult = new ArrayList<>();
        List<OP17DataExport> op17DataExportResult = new ArrayList<>();
        List<OP19DataExport> op19DataExportResult = new ArrayList<>();
        List<OP21DataExport> op21DataExportResult = new ArrayList<>();
        List<OP23DataExport> op23DataExportResult = new ArrayList<>();
        List<OP25DataExport> op25DataExportResult = new ArrayList<>();
        List<OP27DataExport> op27DataExportResult = new ArrayList<>();
        List<OP29DataExport> op29DataExportResult = new ArrayList<>();

        for (MesProductNodeResult item : list) {
            //涂胶线
            if (item.getSubsysCode().equals("2")) {
                if (item.getStationCode().equals("1")) {
                    OP02_1DataFirstExport export = JSON.parseObject(item.getTestDetail(), OP02_1DataFirstExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op02_1DataFirstExportResult.add(export);
                }
                if (item.getStationCode().equals("2")) {
                    OP02_2DataExport export = JSON.parseObject(item.getTestDetail(), OP02_2DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op02_2DataExportResult.add(export);
                }
                if (item.getStationCode().equals("3")) {
                    OP03DataExport export = JSON.parseObject(item.getTestDetail(), OP03DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op03DataExportResult.add(export);
                }
                if (item.getStationCode().equals("4")) {
                    OP04DataExport export = JSON.parseObject(item.getTestDetail(), OP04DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op04DataExportResult.add(export);
                }
                if (item.getStationCode().equals("5")) {
                    OP05DataExport export = JSON.parseObject(item.getTestDetail(), OP05DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op05DataExportResult.add(export);
                }
            }
            //总装线
            if (item.getSubsysCode().equals("1")){
                if (item.getStationCode().equals("27F")) {
                    OP27DataFirstExport export = JSON.parseObject(item.getTestDetail(), OP27DataFirstExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op27DataFirstExportResult.add(export);
                }
                if (item.getStationCode().equals("7")) {
                    OP07DataExport export = JSON.parseObject(item.getTestDetail(), OP07DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op07DataExportResult.add(export);
                }
                if (item.getStationCode().equals("9")) {
                    OP09DataExport export = JSON.parseObject(item.getTestDetail(), OP09DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op09DataExportResult.add(export);
                }
                if (item.getStationCode().equals("11")) {
                    OP11DataExport export = JSON.parseObject(item.getTestDetail(), OP11DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op11DataExportResult.add(export);
                }
                if (item.getStationCode().equals("12")) {
                    OP12DataExport export = JSON.parseObject(item.getTestDetail(), OP12DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op12DataExportResult.add(export);
                }
                if (item.getStationCode().equals("14")) {
                    OP14DataExport export = JSON.parseObject(item.getTestDetail(), OP14DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op14DataExportResult.add(export);
                }
                if (item.getStationCode().equals("15")) {
                    OP15DataExport export = JSON.parseObject(item.getTestDetail(), OP15DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op15DataExportResult.add(export);
                }
                if (item.getStationCode().equals("17")) {
                    OP17DataExport export = JSON.parseObject(item.getTestDetail(), OP17DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op17DataExportResult.add(export);
                }
                if (item.getStationCode().equals("19")) {
                    OP19DataExport export = JSON.parseObject(item.getTestDetail(), OP19DataExport.class);
                    export.setMachineModel(item.getMachineModel());
                    export.setPartNumber(item.getPartNumber());
                    export.setStationCode(item.getStationCode());
                    export.setStatus(item.getStatus());
                    export.setTestResult(item.getTestResult());
                    export.setCreateTime(item.getCreateTime());
                    op19DataExportResult.add(export);
                }
            }
        }

        // 创建Sheet数据列表
        List<ExcelUtil.SheetData<?>> sheetDataList = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(op02_1DataFirstExportResult)) {
            ExcelUtil<OP02_1DataFirstExport> util = new ExcelUtil<OP02_1DataFirstExport>(OP02_1DataFirstExport.class);
            util.exportExcel(response, op02_1DataFirstExportResult, "生产记录数据");
        }
        if (CollectionUtils.isNotEmpty(op02_2DataExportResult)) {
            ExcelUtil<OP02_2DataExport> util = new ExcelUtil<OP02_2DataExport>(OP02_2DataExport.class);
            util.exportExcel(response, op02_2DataExportResult, "生产记录数据");
        }
        if (CollectionUtils.isNotEmpty(op03DataExportResult)) {
            ExcelUtil<OP03DataExport> util = new ExcelUtil<OP03DataExport>(OP03DataExport.class);
            util.exportExcel(response, op03DataExportResult, "生产记录数据");
        }
        if (CollectionUtils.isNotEmpty(op04DataExportResult)) {
            ExcelUtil<OP04DataExport> util = new ExcelUtil<OP04DataExport>(OP04DataExport.class);
            util.exportExcel(response, op04DataExportResult, "生产记录数据");
        }
        if (CollectionUtils.isNotEmpty(op05DataExportResult)) {
            ExcelUtil<OP05DataExport> util = new ExcelUtil<OP05DataExport>(OP05DataExport.class);
            util.exportExcel(response, op05DataExportResult, "生产记录数据");
        }
        if (CollectionUtils.isNotEmpty(op27DataFirstExportResult)) {
            ExcelUtil<OP27DataFirstExport> util = new ExcelUtil<OP27DataFirstExport>(OP27DataFirstExport.class);
            util.exportExcel(response, op27DataFirstExportResult, "生产记录数据");
        }
        if (CollectionUtils.isNotEmpty(op07DataExportResult)) {
            ExcelUtil<OP07DataExport> util = new ExcelUtil<OP07DataExport>(OP07DataExport.class);
            util.exportExcel(response, op07DataExportResult, "生产记录数据");
        }
        if (CollectionUtils.isNotEmpty(op09DataExportResult)) {
            ExcelUtil<OP09DataExport> util = new ExcelUtil<OP09DataExport>(OP09DataExport.class);
            util.exportExcel(response, op09DataExportResult, "生产记录数据");
        }
        if (CollectionUtils.isNotEmpty(op11DataExportResult)) {
            ExcelUtil<OP11DataExport> util = new ExcelUtil<OP11DataExport>(OP11DataExport.class);
            util.exportExcel(response, op11DataExportResult, "生产记录数据");
        }
        if (CollectionUtils.isNotEmpty(op12DataExportResult)) {
            ExcelUtil<OP12DataExport> util = new ExcelUtil<OP12DataExport>(OP12DataExport.class);
            util.exportExcel(response, op12DataExportResult, "生产记录数据");
        }
        if (CollectionUtils.isNotEmpty(op14DataExportResult)) {
            ExcelUtil<OP14DataExport> util = new ExcelUtil<OP14DataExport>(OP14DataExport.class);
            util.exportExcel(response, op14DataExportResult, "生产记数据");
        }
        if (CollectionUtils.isNotEmpty(op15DataExportResult)) {
            ExcelUtil<OP15DataExport> util = new ExcelUtil<OP15DataExport>(OP15DataExport.class);
            util.exportExcel(response, op15DataExportResult, "生产记数据");
        }
        if (CollectionUtils.isNotEmpty(op17DataExportResult)) {
            ExcelUtil<OP17DataExport> util = new ExcelUtil<OP17DataExport>(OP17DataExport.class);
            util.exportExcel(response, op17DataExportResult, "生产记数据");
        }
        if (CollectionUtils.isNotEmpty(op19DataExportResult)) {
            ExcelUtil<OP19DataExport> util = new ExcelUtil<OP19DataExport>(OP19DataExport.class);
            util.exportExcel(response, op19DataExportResult, "节点检测结果数据");
        }
        if (CollectionUtils.isNotEmpty(op21DataExportResult)) {
            ExcelUtil<OP21DataExport> util = new ExcelUtil<OP21DataExport>(OP21DataExport.class);
            util.exportExcel(response, op21DataExportResult, "节点检测结果数据");
        }
        if (CollectionUtils.isNotEmpty(op23DataExportResult)) {
            ExcelUtil<OP23DataExport> util = new ExcelUtil<OP23DataExport>(OP23DataExport.class);
            util.exportExcel(response, op23DataExportResult, "节点检测结果数据");
        }
        if (CollectionUtils.isNotEmpty(op25DataExportResult)) {
            ExcelUtil<OP25DataExport> util = new ExcelUtil<OP25DataExport>(OP25DataExport.class);
            util.exportExcel(response, op25DataExportResult, "节点检测结果数据");
        }
        if (CollectionUtils.isNotEmpty(op27DataExportResult)) {
            ExcelUtil<OP27DataExport> util = new ExcelUtil<OP27DataExport>(OP27DataExport.class);
            util.exportExcel(response, op27DataExportResult, "节点检测结果数据");
        }
        if (CollectionUtils.isNotEmpty(op29DataExportResult)) {
            ExcelUtil<OP29DataExport> util = new ExcelUtil<OP29DataExport>(OP29DataExport.class);
            util.exportExcel(response, op29DataExportResult, "节点检测结果数据");
        }
    }

    /**
     * 获取节点检测结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:result:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mesProductNodeResultService.selectMesProductNodeResultById(id));
    }

    /**
     * 新增节点检测结果
     */
    @PreAuthorize("@ss.hasPermi('order:result:add')")
    @Log(title = "节点检测结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesProductNodeResult mesProductNodeResult) {
        return toAjax(mesProductNodeResultService.insertMesProductNodeResult(mesProductNodeResult));
    }

    /**
     * 修改节点检测结果
     */
    @PreAuthorize("@ss.hasPermi('order:result:edit')")
    @Log(title = "节点检测结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProductNodeResult mesProductNodeResult) {
        return toAjax(mesProductNodeResultService.updateMesProductNodeResult(mesProductNodeResult));
    }

    /**
     * 删除节点检测结果
     */
    @PreAuthorize("@ss.hasPermi('order:result:remove')")
    @Log(title = "节点检测结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mesProductNodeResultService.deleteMesProductNodeResultByIds(ids));
    }

    /**
     * 删除当前和后续节点检测结果
     */
    @Log(title = "删除当前和后续节点检测结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/flow/{ids}")
    public AjaxResult removeFlow(@PathVariable Long[] ids)
    {
        return toAjax(mesProductNodeResultService.deleteMesProductNodeFlowResultByIds(ids));
    }
}
